package com.pic4493.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.pic4493.adapter.AdpCstLayListView;
import com.pic4493.base.BaseUITopBar;
import com.pic4493.controls.CstLayoutListView;
import com.pic4493.entities.DJson;
import com.pic4493.entities.ESubscribe;
import com.pic4493.net.NetClientJ;
import com.pic4493.utils.UtiDialog;

public class UISubscribe extends BaseUITopBar
{

   private CstLayoutListView mListViewSub;
   private CstLayoutListView mListViewUnsub;

   private ArrayList<ESubscribe> mListSub;
   private ArrayList<ESubscribe> mListUnSub;

   private AdpCstLayListView mAdpSub;
   private AdpCstLayListView mApdUnSub;

   private void initView()
   {
	  mListViewSub = (CstLayoutListView) findViewById(R.id.uisub_list_sub);
	  mListViewUnsub = (CstLayoutListView) findViewById(R.id.uisub_list_unsub);
   }

   private void initListData()
   {
	  executeAsyncTask("checksubs");
   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  setContentView(R.layout.uisub);
	  super.onCreate(savedInstanceState);
	  setTitle("订阅管理");

	  initView();
	  initListData();
   }

   private ArrayList<Map<String, Object>> getSubs(ArrayList<ESubscribe> subList)
   {
	  ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	  Map<String, Object> map = null;
	  int size = subList.size();
	  for (int i = 0; i < size; i++)
	  {
		 map = new HashMap<String, Object>();
		 map.put("name", subList.get(i).getName());
		 list.add(map);
	  }
	  return list;
   }

   @Override
   public void onAsyncTaskStart(String taskName, Object[] params)
   {
	  UtiDialog.getInstance().ShowLoadingDialogNoTitleEX(mContext, "加载中...");
   }

   @Override
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  if ("checksubs".equals(taskName))
	  {
		 return NetClientJ.GetUserSubscribes();
	  }
	  else if ("sub".equals(taskName))
	  {
		 return NetClientJ.AddSubscribe(params[0].toString());
	  }
	  else if ("unsub".equals(taskName))
	  {
		 return NetClientJ.DeleteSubscribe(params[0].toString());
	  }
	  return null;
   }

   @Override
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {
	  UtiDialog.getInstance().DismissLoadingDialog();
	  try
	  {
		 if (result != null)
		 {
			DJson data = (DJson) result;
			if (!data.getHead().hasError())
			{
			   if ("checksubs".equals(taskName))
			   {
				  JSONObject object = data.getBody();
				  JSONArray subs = object.optJSONArray("Order");
				  JSONArray unsubs = object.optJSONArray("NotOrder");

				  ESubscribe sub = null;

				  // 组装已订阅的列表
				  mListSub = new ArrayList<ESubscribe>(); 
				  mAdpSub = null;
				  mListViewSub.cleanData();
				  if (subs != null)
				  { 
					 for (int i = 0; i < subs.length(); i++)
					 {
						sub = new ESubscribe();
						sub.setId(subs.getJSONObject(i).optString("sid"));
						sub.setName(subs.getJSONObject(i).optString("ztname"));
						mListSub.add(sub);
					 }
					 mAdpSub = new AdpCstLayListView(mContext, getSubs(mListSub), R.layout.uisub_list_item_sub, new String[] { "name" }, new int[] { R.id.uisub_list_item_sub_txt_name },
						   R.id.uisub_list_item_sub_txt_btn, new View.OnClickListener()
						   {
							  @Override
							  public void onClick(View v)
							  {
								 // 已订阅列表点击是取消订阅
								 executeAsyncTask("unsub", new Object[] { mListSub.get(Integer.valueOf(v.getTag().toString())).getId() });
							  }
						   });
					 mListViewSub.setAdapter(mAdpSub);
				  }
				  
				  // 组装未订阅的列表
				  mListUnSub = new ArrayList<ESubscribe>();
				  mApdUnSub = null;			
				  mListViewUnsub.cleanData();
				  if (unsubs != null)
				  { 
					 for (int i = 0; i < unsubs.length(); i++)
					 {
						sub = new ESubscribe();
						sub.setId(unsubs.getJSONObject(i).optString("sid"));
						sub.setName(unsubs.getJSONObject(i).optString("ztname"));
						mListUnSub.add(sub);
					 } 
					 mApdUnSub = new AdpCstLayListView(mContext, getSubs(mListUnSub), R.layout.uisub_list_item_unsub, new String[] { "name" }, new int[] { R.id.uisub_list_item_unsub_txt_name },
						   R.id.uisub_list_item_unsub_txt_btn, new View.OnClickListener()
						   {
							  @Override
							  public void onClick(View v)
							  {
								 // 为订阅列表点击是添加订阅
								 executeAsyncTask("sub", new Object[] { mListUnSub.get(Integer.valueOf(v.getTag().toString())).getId() });
							  }
						   });
					 mListViewUnsub.setAdapter(mApdUnSub);
				  }
			   }
			   else if ("sub".equals(taskName))
			   { // 用于在返回首页是告诉首页已经变更
				  setResult(RESULT_OK);
				  initListData();
			   }
			   else if ("unsub".equals(taskName))
			   {
				  setResult(RESULT_OK);
				  initListData();
			   }
			}
			else
			{
			   UtiDialog.getInstance().ShowToastInfoShort(mContext, data.getHead().getRspMsg());
			}
		 }
		 else
		 {
			UtiDialog.getInstance().ShowToastInfoShort(mContext, "网络异常请稍候再试");
		 }
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UISubscribe_onAsyncTaskEnd(taskId, taskName, params, result) " + e);
	  }
   }
}
